package com.yc.bank.web;

import com.google.gson.Gson;
import com.yc.bank.biz.AccountBiz;
import com.yc.bank.entity.Account;
import com.yc.bank.entity.Result;
import com.yc.bank.service.IAccountService;
import com.yc.bank.service.IOprecordService;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.naming.Name;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DePositServlet", value = "/dePosit.s")
public class DePositServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        IAccountService accountService = ctx.getBean(IAccountService.class);
        IOprecordService oprecordService = ctx.getBean(IOprecordService.class);

        String name = (String) request.getSession().getAttribute("name");
        String money = request.getParameter("money");
        String remark = "存款";

        AccountBiz accountBiz = new AccountBiz();
        accountBiz.deposit(name, Double.parseDouble(money),remark,accountService,oprecordService);

        Result result = null;
        result = new Result(1,"存款成功", null);
        String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }
}